package piccat.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import javax.swing.JPanel;

class GridPanel extends JPanel {

    private static final Color[] FILL_COLOURS =
            {new Color(191, 191, 255), new Color(255, 255, 127),
            new Color(255, 127, 255)};
    private static final Color[] BORDER_COLOURS =
            {new Color(127, 127, 255), new Color(255, 255, 63),
            new Color(255, 63, 255)};

    public GridPanel() {
        super();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        if (this.getLayout() == null) return;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        if (this.getLayout().getClass() != GridLayout.class) return;
        GridLayout layout = (GridLayout) this.getLayout();
        int size = 4;
        int rows = layout.getRows();
        int cols = layout.getColumns();
        int rowHeight = getHeight() / rows;
        int colWidth = getWidth() / cols;
        int arc = Math.min(colWidth, rowHeight) / 2;
        g2.setStroke(new BasicStroke(size));
        for (int c = 0; c < cols;
                c++) {
            int x = c * colWidth + size;
            int y = 0 + size;
            int w = colWidth - (size * 2);
            int h = getHeight() - (size * 2);
            g2.setColor(FILL_COLOURS[c % FILL_COLOURS.length]);
            g2.fillRoundRect(x, y, w, h, arc, arc);
            g2.setColor(BORDER_COLOURS[c % BORDER_COLOURS.length]);
            g2.drawRoundRect(x, y, w, h, arc, arc);
        }
    }
}
